package com.zfzs.post.modules.banner.service.impl;

import com.zfzs.post.modules.banner.domain.BannerInfo;
import com.zfzs.post.modules.banner.repository.BannerInfoRepository;
import com.zfzs.post.modules.banner.service.BannerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * banner图
 *
 * @author: ck
 * @create: 2019/8/20/15:16
 **/
@Service
public class BannerInfoServiceImpl implements BannerInfoService {

    @Autowired
    private BannerInfoRepository bannerRepository;

    @Override
    public BannerInfo findOne(String id) {
        Optional<BannerInfo> p = bannerRepository.findById(id);
        if (!p.isPresent()) {
            return null;
        }
        return p.get();
    }

    @Override
    public BannerInfo save(BannerInfo bannerInfo) {
        return bannerRepository.save(bannerInfo);
    }

    @Override
    public List<BannerInfo> queryAll() {
        return bannerRepository.findAll();
    }

    @Override
    public List<BannerInfo> queryBannerList(BannerInfo bannerInfo) {
        //构建对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("types", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<BannerInfo> ex = Example.of(bannerInfo, matcher);
        List<BannerInfo> list = bannerRepository.findAll(ex, new Sort(Sort.Direction.DESC, "store"));
        return list;
    }
}
